var abnormalRecordData = [];
var totalPage = 0;
var totalCnt = 0;
var deleteFlag = undefined;
var exportIndex = undefined;

//查询
$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	selectDate("startDate");
	selectDate("endDate");
	createTableHead("abnormalRecordThead", abnormalRecordTable, search);
	scrollTableSuspend("scrollBody", "abnormalRecordTable", "abnormalRecordTable1", "abnormalRecordThead", "scrollHeight");
	$ajaxList('chargingStation', getChargingStation, {BulidStatus: '3'});
});
function firstSearch(){
	var passValuesNum = window.parent.document.getElementById("passValue").value;
    if(passValuesNum != ""){
    	 $("#chargingStationID").val(passValuesNum);
    	 window.parent.document.getElementById("passValue").value = '';
    }
	search();
}
function getChargingStation(data) {
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#chargingStationID").append("<option value='" + data[i].chargingStationID + "'>" + data[i].chargingStationName + "</option>");
	}
	if($("#detailID", parent.document).val() != ""){
		var equipmentID = $("#detailID", parent.document).val();
		var chargingStationID = equipmentID.substr(0, 3);
		$('#chargingStationID').val(chargingStationID);
		changeEquipmet();
	} else {
		search();
	}
}
function changeEquipmet(){
	var chargingStationID = $("#chargingStationID").val();
	if (chargingStationID) {
		$("#changeEquipment").show();
		$("#equipmentNo").empty();
		$ajaxList('equipmentNo', getEquipmentNo, {"chargingStationID": chargingStationID});
	} else {
		$("#changeEquipment").hide();
		$("#equipmentNo").empty();
	}
}
function getEquipmentNo(data) {
	var length = data.length;
	$("#equipmentNo").append("<option value=''>请选择</option>");
	for (var i = 0; i < length; i++) {
		$("#equipmentNo").append("<option value='" + data[i].equipmentNo + "'>" + data[i].equipmentName + "</option>");
	}
	$('#equipmentNo').val($("#detailID", parent.document).val());
	$("#detailID", parent.document).val("");
	search();
}
function search(){
	var startTime = $("#startTime", parent.document).val();
	var endTime = $("#endTime", parent.document).val();
	if(startTime){
		$("#startChargingTime").val(startTime.substring(0, 11));
		$("#endChargingTime").val(endTime.substring(0, 11));
		$("#startTime", parent.document).val("");
		$("#endTime", parent.document).val("");
		$("#back").attr("hidden", false);
	}
	var startChargingTime = $("#startChargingTime").val();
	var endChargingTime = $("#endChargingTime").val();
	if (stringToDate(startChargingTime) > stringToDate(endChargingTime)){
		showMessge("开始时间不能大于结束时间！");
	} else {
		$ajax('searchAbnormalRecord', 'abnormalRecordSearch', searchAbnormalRecord);
	}
}
function searchAbnormalRecord(data) {
	totalPage = data && data.totalPage || 0;
	totalCnt = data && data.totalCnt || 0;
	abnormalRecordData = data && data.abnormalRecordList || [];
	createTableBody("abnormalRecordTbody", abnormalRecordTable, abnormalRecordData);
	for (var i = 0, length = abnormalRecordData.length; i < length; i++) {
		if (abnormalRecordData[i].chargingProcessFile) {
			$('#chargingProcessFileFlag' + i).html('<img src="static/images/fileExistZip.png" style="width:25px;height:25px;" />');
			$('#chargingProcessFileFlag' + i).attr("onclick","exportProcess(" + i + ");return false;");
		} else {
			$('#chargingProcessFileFlag' + i).html('<img src="static/images/fileNotExistZip.png" style="width:25px;height:25px;" />');
		}
	}
	setTimeout(hideLoading, 500);
}
function seeMessage(index){
	if (abnormalRecordData[index].message != null){
		$("#message").val(abnormalRecordData[index].message.replace(/,/g, "\r\n"));
	} else {
		$("#message").val('充电报文不存在');
	}
	$("#Message").modal("show");
}
function messageSure(){
	$("#Message").modal("hide");
}
//修改
function update() {
	var checkbox = getCheckbox("chk");
	var count = checkbox && checkbox.count;
	var index = checkbox && checkbox.row_index;
    if (count == 1) {
    	$('#title').html('');
    	$("#ID").val(abnormalRecordData[index].id);
    	deleteFlag = "update";
    	addTitle("chargingRecordDetails");
    	allTable(index);
    } else if (count == 0) {
    	showMessge(messageContent["Select.NotFound"]);
    } else {
    	showMessge(messageContent["Select.OnlyOne"]);
    }
}
function allTable(index){
	createDetailHead("detailThead", abnormalRecordTable, 1);
	createDetailBody("detailTbody", abnormalRecordTable, abnormalRecordData[index], 1);
	createDetailAll("detailTable", abnormalRecordTable, abnormalRecordData[index], 2);
	if(deleteFlag == "details"){
		setFormDisabled("saveForm");
		$('#confirm').hide();
	} else {
		$('#confirm').show();
	}
	$("#save").modal("show");
}
function confirm(){
	return checkForm("saveForm", abnormalRecordTable);
}
function getUrl(){
	if(deleteFlag == "update"){
		return 'updateAbnormalRecord';
	} else if(deleteFlag == "delete"){
		return 'deleteAbnormalRecord';
	}
}
function getData(){
	return {chargingRecordList: getCheckDataList(abnormalRecordData, "id")};
}
function saveAbnormalRecord(data){
	var result = data && data.result;
	if (result == "Success"){
		showMessge(messageContent["Update.Success"]);
		$('#save').modal('hide');
		formReset("saveForm");
	} else if (result == "NotFound"){
		showMessge(messageContent["Update.Failed"]);
	} else{
		showMessge(messageContent["Update.Error"]);
	}
	search();
}
function sure(data){
	if(deleteFlag == "delete") {
		var result = data && data.result;
	    if (result == "Success") {
			showMessge(messageContent["Delete.Success"]);
		} else if (result == "NotFound") {
			showMessge(messageContent["Delete.NotFound"]);
		} else {
			showMessge(messageContent["Delete.Error"]);
		}
	    search();
	} else if(deleteFlag == "export"){
		showMessge("导出中......");
		$ajax('exportAbnormalRecord?checkFlag=', undefined, exportFileResult);
	} else if(deleteFlag == "exportProcess"){
		showMessge("下载中......");
		var url = 'exportChargingProcess?subfolder=' + (abnormalRecordData[exportIndex] && abnormalRecordData[exportIndex].chargingStationID) +
		'&&fileName=' + (abnormalRecordData[exportIndex] && abnormalRecordData[exportIndex].chargingProcessFile) +
		'&&checkFlag=';
		$ajax(url, undefined, exportProcessResult);
	}
}
function exportFile(){
	if (abnormalRecordData.length > 0) {
		showMessge("确定导出吗?", "model2");
		deleteFlag = "export";
	} else {
		showMessge("没有数据可导出！");
	}
}
function exportFileResult(data){
	var result = data && data.result;
	if (result == "Success") {
		showLoading();
		downloadFile('exportAbnormalRecord?checkFlag=OK');
		showMessge("导出成功，充电记录文件正在生成！");
		setTimeout(hideLoading, 2000);
	} else if (result == "JsonError") {
		showMessge("导出失败，导出的JSON模板错误 或 不存在！");
	} else if (result == "NotFound") {
		showMessge("导出失败，没有数据可导出！");
	} else {
		showMessge("导出失败，请重新导出 或 联系管理员！");
	}
}
function exportProcess(index){
	showMessge("确定下载吗?", "model2");
	exportIndex = index;
}
function exportProcessResult(data){
	var result = data && data.result;
	if (result == "Success") {
		showLoading();
		var url = 'exportChargingProcess?subfolder=' + (abnormalRecordData[exportIndex] && abnormalRecordData[exportIndex].chargingStationID) +
		'&&fileName=' + (abnormalRecordData[exportIndex] && abnormalRecordData[exportIndex].chargingProcessFile) +
		'&&checkFlag=OK';
		downloadFile(url);
		showMessge("下载成功，过程文件正在生成！");
		setTimeout(hideLoading, 2000);
	} else if (result == "NotFound") {
		showMessge("下载失败，过程文件不存在!");
	} else {
		showMessge("下载失败，请重新下载 或 联系管理员！");
	}
}
function goback() {
	$("#detailID", parent.document).val("backFlag");
	window.parent.detalisSkip('chargingEquipment/abnormalStatistics');
}